﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Butcher.winform.EnterpriseForms.RBAC.RoleList;
using Traceability.Commont.Domains;
using static Butcher.winform.EnterpriseForms.DeductionHtml;
using System.Text.Json;
using Traceability.Commont.DTOs.UsersFile;

namespace Butcher.winform.EnterpriseForms.RBAC
{
    public partial class UserList : Form
    {
        public UserList()
        {
            InitializeComponent();
            NEWHtml();
        }
        public class usersd
        {
            public string? UserName { get; set; }

            public int pageIndex { get; set; }

            public int pageSize { get; set; }
            //public string lists { get; set; }
            //public DateTime createTime { get; set; } = DateTime.Now;
            //public int stuct { get; set; }
            //public string pwd { get; set; }
            //public int userId { get; set; }
        }
        usersd user = new usersd();
        public string? UserName = "";
        public int pageIndex = 1;
        public int pageSize = 3;
        public int totalCount = 0;
        public int pageCount = 0;
        private void NEWHtml()
        {
            UserListDTO users = new UserListDTO();
            user.UserName = UserName;
            user.pageIndex = pageIndex;
            user.pageSize = pageSize;
            string url = @"http://localhost:5021/api/Trace/getUserList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<usersd>(user), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var aaa = info["lists"].Value<JArray>();

            var counts = info["totalCount"];
            var pages = info["pageCount"];
            totalCount = Convert.ToInt32(counts);
            pageCount = Convert.ToInt32(pages);
            List<UserListDTO> cts = aaa.ToObject<List<UserListDTO>>();
            foreach (var ct in cts)
            {
                dataGridView1.DataSource = cts;
            }

            textBox2.Text = pageIndex.ToString();
            textBox3.Text = totalCount.ToString();
        }
        private void UserList_Load(object sender, EventArgs e)
        {
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            NEWHtml();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pageIndex == 1)
            {
                MessageBox.Show("到首页了");
            }
            else
            {
                pageIndex -= 1;
            }
            NEWHtml();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pageIndex == totalCount)
            {
                MessageBox.Show("到未页了");
            }
            else
            {
                pageIndex += 1;
            }
            NEWHtml();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            pageIndex = totalCount;
            NEWHtml();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
